package com.swq.java.code.chapter13.code33;

class Counter {
    private static int count;
    // 这里是java定义的ThreadLocal，书上的例子在code32中
    private static ThreadLocal<Integer> serialCount = new ThreadLocal<Integer>() {

        protected synchronized Integer initialValue() {
            return new Integer(count++);
        }
    };

    public static int get() {
        return serialCount.get();
    }

    public static void set(int i) {
        serialCount.set(i);
    }
}

public class LocalTester extends Thread {
    public void run() {
        for (int i = 0; i < 3; i++) {
            int c = Counter.get();
            System.out.println(getName() + ":" + c);
            Counter.set(c += 2);
        }
    }

    public static void main(String args[]) {
        Thread t1 = new LocalTester();
        Thread t2 = new LocalTester();
        t1.start();
        t2.start();
    }
}